package com.xxchat.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.xxchat.entity.constants.RedisConstants;
import com.xxchat.entity.constants.UserInfoConstants;
import com.xxchat.entity.vo.CheckCodeVO;
import com.xxchat.entity.vo.ResponseVO;
import com.xxchat.entity.vo.UserInfoVO;
import com.xxchat.redis.RedisComponent;
import com.xxchat.redis.RedisUtils;
import com.xxchat.service.impl.UserInfoServiceImpl;
import com.xxchat.utils.IPAreaUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.UUID;

/**
 * @author Waicc
 */
@RestController("accountController")
@RequestMapping("/account")
@Validated  // 参数校验
public class AccountController extends ABaseController {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private UserInfoServiceImpl userInfoService;

    // 获取验证码
    @RequestMapping("/checkCode")
    public ResponseVO checkCode() {
        // 生成验证码
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 43);
        // 验证码
        String code = captcha.text();
        // 验证码图片
        String checkCodeBase64 = captcha.toBase64();
        // 验证码key
        String checkCodeKey = UUID.randomUUID().toString();

        // 将验证码放入缓存
        String redisKey = RedisConstants.REDIS_KEY_CHECK_CODE + checkCodeKey;
        redisUtils.setex(redisKey, code, RedisConstants.REDIS_EXPIRE_TIME_ONE_MIN);

        // 封装CheckCode
        CheckCodeVO checkCode = new CheckCodeVO();
        checkCode.setCheckCodeBase64(checkCodeBase64);
        checkCode.setCheckCodeKey(checkCodeKey);

        return getSuccessResponseVO(checkCode);
    }

    // 注册
    @RequestMapping("/register")
    public ResponseVO register(@NotEmpty String checkCodeKey,
                               @NotEmpty @Email(message = "邮箱格式不正确") @Size(max = 50, message = "邮箱长度不得超过50个字符") String email,
                               @NotEmpty @Pattern(regexp = UserInfoConstants.REGEX_PASSWORD) @Size(max = 32, message = "密码长度不得超过32个字符") String password,
                               @NotEmpty @Size(min = 2, max = 20, message = "用户名长度必须在2到20个字符之间") String nickName,
                               @NotEmpty String checkCode) {
        userInfoService.register(checkCodeKey, email, password, nickName, checkCode);
        return getSuccessResponseVO("注册成功");
    }

    // 登录
    @RequestMapping("/login")
    public ResponseVO login(@NotEmpty String checkCodeKey,
                            @NotEmpty @Email(message = "邮箱格式不正确") @Size(max = 50, message = "邮箱长度不得超过50个字符") String email,
                            @NotEmpty @Size(max = 32, message = "密码长度不得超过32个字符") String password,
                            @NotEmpty String checkCode,
                            HttpServletRequest request) {
        String ip = IPAreaUtils.getClientIp(request);
        UserInfoVO userInfoVO = userInfoService.login(checkCodeKey, email, password, checkCode, ip);

        return getSuccessResponseVO(userInfoVO);
    }

    // 获取系统配置
    @RequestMapping("/getSystemSetting")
    public ResponseVO getSystemSetting() {
        return getSuccessResponseVO(redisComponent.getSysSetting());
    }
}
